package c.mars.tower_of_hanoi.game;

import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
import android.content.Context;

public class GameGLSurfaceView extends GLSurfaceView {

	private static final float TOUCH_SCALE_FACTOR = 180.0f / 320;
	private GameRenderer mRenderer;
	private float mPreviousX;
    private float mPreviousY;
	
	public GameGLSurfaceView(Context context){
		super(context);

//		Rendering options
		setEGLContextClientVersion(2);
//		setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); // This leads to crash on my device
		
//		Create and remember Renderer for drawing on the GLSurfaceView
		mRenderer = new GameRenderer();
		setRenderer(mRenderer);
	}

	@Override
	public boolean onTouchEvent(MotionEvent e) {
		// MotionEvent reports input details from the touch screen
		// and other input controls. In this case, you are only
		// interested in events where the touch position changed.

		float x = e.getX();
		float y = e.getY();

		switch (e.getAction()) {
		case MotionEvent.ACTION_MOVE:

			float dx = x - mPreviousX;
			float dy = y - mPreviousY;

			// reverse direction of rotation above the mid-line
			if (y > getHeight() / 2) {
				dx = dx * -1 ;
			}

			// reverse direction of rotation to left of the mid-line
			if (x < getWidth() / 2) {
				dy = dy * -1 ;
			}

			mRenderer.setAngle(
					mRenderer.getAngle() +
					((dx + dy) * TOUCH_SCALE_FACTOR));  // = 180.0f / 320
					requestRender();
		}

		mPreviousX = x;
		mPreviousY = y;
		return true;
	}

}


